package NC113_验证IP地址;

import java.util.*;


public class Solution {
    /**
     * 验证IP地址
     * @param IP string字符串 一个IP地址字符串
     * @return string字符串
     */
    public String solve (String IP) {
        // write code here
        return validIPV4(IP) ? "IPv4" : (validIPV6(IP) ? "IPv6" : "Neither");
    }

    private boolean validIPV6(String ip) {
        String[] str = ip.split(":", -1);
        if (str.length != 8){
            return false;
        }

        for (String s : str){
            if (s.length() > 4 || s.length() == 0){
                return false;
            }
            try {
                int val = Integer.parseInt(s, 16);
            }catch (NumberFormatException numberFormatException){
                return false;
            }
        }
        return true;
    }

    private boolean validIPV4(String ip) {
        String[] str = ip.split("\\.", -1);
        if (str.length != 4){
            return false;
        }
        for (String s : str){
            if (s.length() > 1 && s.startsWith("0")){
                return false;
            }
            try {
                int val = Integer.parseInt(s);
                if (val < 0 || val > 255) {
                    return false;
                }
            } catch (NumberFormatException numberFormatException) {
                return false;
            }
        }
        return true;
    }
}